﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using Models;
using Common;

namespace DAL
{
    /// <summary>
    /// 开放方向数据处理
    /// </summary>
    public class KaikongDAL
    {
        //展示列表
        public DataTable ListAll(string direction) {
            string sql = string.Empty;
            if (!string.IsNullOrWhiteSpace(direction)) {
                sql += " and direction like '%" + direction + "%'";
            }
            return SqlHelper.ExecuteDataTable("select * from kkfx where isdel =@isdel "+sql+" order by edittime desc",new MySqlParameter("@isdel","0"));
            
        }
        //根据id删除
        public int DelById(string kid) {
            return SqlHelper.ExecuteNonQuery("update kkfx set isdel =1,editTime=@editTime,editUser=@editUser where kid = @kid",
                new MySqlParameter("@editTime", DateTime.Now.ToString()), new MySqlParameter("@editUser", RemeberName.Username), new MySqlParameter("@kid", kid));
        }
        //根据id查询
        public Kaikong GetById(string kid) {
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataTable("select * from kkfx where kid=@kid",new MySqlParameter("@kid",kid));
            if (dt.Rows.Count==1)
            {
                return ToKaikong(dt.Rows[0]);
            }
            else if (dt.Rows.Count < 1)
            {
                return null;
            }
            else {
                throw new Exception("存在重复值");
            }
        }
        //转换成模型
        public Kaikong ToKaikong(DataRow dr) {
            Kaikong kk = new Kaikong();
            kk.Kid = Convert.ToInt32(dr["kid"]);
            kk.Direction = dr["direction"].ToString();
            kk.Isdel = Convert.ToInt32(dr["isdel"]);
            kk.Bzsm = dr["bzsm"].ToString();
            return kk;
        }
        //更新数据
        public int UpdateData(Kaikong kk) {
            return SqlHelper.ExecuteNonQuery("update kkfx set direction=@direction,bzsm=@bzsm,editTime=@editTime,editUser=@editUser  where kid =@kid",
                new MySqlParameter("@direction",kk.Direction),new MySqlParameter("@bzsm",kk.Bzsm),
                new MySqlParameter("@editTime", DateTime.Now.ToString()), new MySqlParameter("@editUser", RemeberName.Username), new MySqlParameter("@kid", kk.Kid));
        }
        //新增数据
        public int SaveData(Kaikong kk) {
            return SqlHelper.ExecuteNonQuery("insert into kkfx (direction,bzsm,isdel,addTime,editTime,addUser,editUser) values(@direction,@bzsm,@isdel,@addTime,@editTime,@addUser,@editUser)",
                new MySqlParameter("@direction",kk.Direction), new MySqlParameter("bzsm",kk.Bzsm),new MySqlParameter("isdel","0"),new MySqlParameter("@addTime", DateTime.Now.ToString()), 
                new MySqlParameter("@editTime", DateTime.Now.ToString()), new MySqlParameter("@addUser", RemeberName.Username), new MySqlParameter("@editUser", RemeberName.Username));
        }
    }
}
